<?php
/**
 * @func 微信机器人,主要控制类
 * Author: Midoks
 * Author URI: http://midoks.cachecha.com/
 */

class weixin_robot{

	public $kw = '';//关键字
  	public $fromUserName = '';//请求来用户名
  	public $toUserName = '';//要发送到的用户名
	public $time = '';//时间戳值
  
  	public $tpl;	//模板资源

	//@func 机器人功能验证和返回信息
	public function valid(){
		if(isset($_GET['debug'])){
			$this->kw = strtolower(trim($_GET['kw']));
			//$this->checkSignature();
			$this->responseMsg();
		}else{
			if($this->checkSignature()){
				$echoStr = (isset($_GET['echostr']))?$_GET['echostr']:'';
				echo $echoStr;
                $this->responseMsg();
			}
		}
        exit;
	}

	//检查
	private function checkSignature(){
        $signature = $_GET['signature'];
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['nonce'];	
        		
		$token = WEIXIN_TOKEN;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );	
      
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}

	//返回的信息
	public function responseMsg(){
        //实例化消息模板类
      	include_once(WEIXIN_ROOT_LIB.'weixin_robot_tpl.php');
      	$this->tpl = new weixin_robot_tpl();
      
		$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];//post数据
		//提交后的数据
		if(!empty($postStr)){
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$this->fromUserName = $postObj->FromUserName;
        	$this->toUserName = $postObj->ToUserName;
            $this->kw = trim($postObj->Content);
			$this->time = time();
            //控制消息发送
          	$this->toMsg();
		}else{
            //显示模拟信息
          	//测试地址:midoks.cachecha.com/?midoks&debug=1
        	if(isset($_GET['debug'])){
              	$this->fromUserName = 'midoks';
				$this->toUserName = 'midoks';
				if(isset($_GET['kw'])){
					$this->kw = $_GET['kw'];
				}else{
					$this->kw = 'midoks';
				}
				$this->time = time();
              	//echo 'Success';
              	//控制消息发送
          		$this->toMsg();
            }
        }
	}
  
   /**
    *	@func 控制消息发送
    */
  	public function toMsg(){
		if(!empty($this->kw)){
			/*
            $contentStr = "欢迎使用midoks微信公众平台!,正在开发中.d..!!";
          	$pic = $this->bigPic();
        	
          	$textPic = array(
            				array(
                            	'title'=> '标题',
                              	'desc'=> '描述',
                              	'pic'=> $pic,//图片地址
                              	'link'=>$pic,//图片链接地址
                            ),//第一个图片为大图
              				array(
                            	'title'=> '标题',
                              	'desc'=> '描述',
                              	'pic'=> $this->smallPic(),//图片地址
                              	'link'=> '',//图片链接地址
                            ),//此自以后皆为小图
              				array(
                            	'title'=> '标题',
                              	'desc' => '描述',
                              	'pic'  => $this->smallPic(),//图片地址
                              	'link' => '',//图片链接地址
                            ),
              				array(
                            	'title'=> '标题',
                              	'desc' => '描述',
                              	'pic'  => $this->smallPic(),//图片地址
                              	'link' => '',//图片链接地址
                            ),
              				array(
                            	'title'=> '标题',
                              	'desc' => '描述',
                              	'pic'  => $this->smallPic(),//图片地址
                              	'link' => '',//图片链接地址
                            ),
              				array(
                            	'title'=> '标题',
                              	'desc' => '描述',
                              	'pic'  => $this->smallPic(),//图片地址
                              	'link' => '',//图片链接地址
                            ),
            		   );
          
			*/
            /*$resultStr = sprintf($this->tpl->text(),
                                 $this->fromUserName,
                                 $this->toUserName,
                                 $this->time,
                                 $msgType,
								 $contentStr);*/
          	//echo $this->toMsgText($contentStr);//文本
          	//echo $this->toMsgPic($pic);//图
          	//echo $this->toMsgTextPic($textPic);//图文
          	//$MusicUrl = WEIXIN_ROOT_VOICE.'1.mp3';
			//echo $this->toMsgVoice('声音','当男人好难！', $MusicUrl, $MusicUrl);//voice
			

			//以上为测试和实例
			include(WEIXIN_ROOT_LIB.'weixin_cmd.php');
			$cmd = new weixin_cmd($this);
			echo $cmd->cmd();

        }else{
            echo "Input something...";
        }
  	}
  
   /**
	* @func 返回文本信信息
	* @param $Msg 信息
	* @ret string xml
	*/
	public function toMsgText($Msg){
		$resultStr = sprintf($this->tpl->text(),
                             $this->fromUserName,
                             $this->toUserName,
                             $this->time,
                             'text',
							 $Msg);
		return $resultStr;
	}
  
   /**
	* @func 返回图片信息(测试未成功)
	* @param $pic 图片信息
	* @ret string xml
	*/
  	public function toMsgPic($Pic){
  		$resultStr = sprintf($this->tpl->pic(),
                             $this->fromUserName,
                             $this->toUserName,
                             $this->time,
                             'image',
							 $Pic);
		return $resultStr;
  	}
  	
   /**
	* @func 返回图文
	* @param array $info
	* @ret string xml
	*/
  	public function toMsgTextPic($info){
  		return $this->tpl->textpic($info, $this);
  	}
  
  	//大图片地址
  	public function bigPic(){
  		return WEIXIN_ROOT_NA.'640_320/'.mt_rand(1,6).'.jpg';
    }
  
  	//小图片地址
  	public function smallPic(){
  		return WEIXIN_ROOT_NA.'80_80/'.mt_rand(1,11).'.jpg';
  	}
  
   /**
	* @func 返回voice xml
	* @param $title //标题
	* @param $desc //描述
	* @param $MusicUrl //地址
	* @param $HQMusicUrl //高清播放(会首先选择)
	* @ret string xml
	*/
  	public function toMsgVoice($title, $desc, $MusicUrl, $HQMusicUrl){
    	$resultStr = sprintf($this->tpl->voice(),
                             $this->fromUserName,
                             $this->toUserName,
                             $this->time,
                             $title,//标题
                             $desc,//描述 
                             $MusicUrl,//地址
                             $HQMusicUrl//高清播放(会首先选择)
                            );
		return $resultStr;
  	}
  

}
?>
